


<!DOCTYPE html>
<html lang="zh-CN">
  <head><meta name="generator" content="Hexo 3.8.0">
    <title>创建一个基于 CMS 的博客 — Vue.js</title>
    <meta charset="utf-8">
    <meta name="description" content="Vue.js - The Progressive JavaScript Framework">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="alternate" hreflang="x-default" href="https://vuejs.org/v2/cookbook/serverless-blog.html">
    <link rel="alternate" hreflang="zh" href="serverless-blog.html">
    <link rel="alternate" hreflang="ja" href="https://jp.vuejs.org/v2/cookbook/serverless-blog.html">
    <link rel="alternate" hreflang="ru" href="https://ru.vuejs.org/v2/cookbook/serverless-blog.html">
    <link rel="alternate" hreflang="ko" href="https://kr.vuejs.org/v2/cookbook/serverless-blog.html">
    <link rel="alternate" hreflang="pt-BR" href="https://br.vuejs.org/v2/cookbook/serverless-blog.html">
    <link rel="alternate" hreflang="fr" href="https://fr.vuejs.org/v2/cookbook/serverless-blog.html">

    <meta property="og:type" content="article">
    <meta property="og:title" content="创建一个基于 CMS 的博客 — Vue.js">
    <meta property="og:description" content="Vue.js - The Progressive JavaScript Framework">
    <meta property="og:image" content="https://cn.vuejs.org//images/logo.png">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="创建一个基于 CMS 的博客 — Vue.js">
    <meta name="twitter:description" content="Vue.js - The Progressive JavaScript Framework">
    <meta name="twitter:image" content="https://cn.vuejs.org/images/logo.png">

    <link rel="apple-touch-icon" sizes="57x57" href="../../images/icons/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="../../images/icons/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="../../images/icons/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="../../images/icons/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="../../images/icons/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="../../images/icons/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="../../images/icons/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="../../images/icons/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="../../images/icons/apple-icon-180x180.png">
    <link rel="icon" type="image/png" sizes="192x192" href="../../images/icons/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="../../images/icons/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="96x96" href="../../images/icons/favicon-96x96.png">
    <link rel="icon" type="image/png" sizes="16x16" href="../../images/icons/favicon-16x16.png">
    <meta name="msapplication-TileImage" content="/images/icons/ms-icon-144x144.png">
    <link rel="icon" href="../../images/logo.png" type="image/png">

    <meta name="msapplication-TileColor" content="#4fc08d">
    <meta name="theme-color" content="#4fc08d">

    <meta name="msapplication-config" content="browserconfig.xml">
    <link rel="manifest" href="../../manifest.json">

    <!-- <link href="https://fonts.googleapis.com" rel="preconnect" crossorigin> -->
    <!-- <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin> -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com" rel="preconnect" crossorigin> -->

    <!-- <link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600|Roboto Mono' rel='stylesheet' type='text/css'> -->
    <!-- <link href='//fonts.googleapis.com/css?family=Dosis:500&text=Vue.js' rel='stylesheet' type='text/css'> -->

    <link href="https://code.bdstatic.com/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- main page styles -->
    <link rel="stylesheet" href="../../css/page.css">

    <!-- this needs to be loaded before guide's inline scripts -->
    <script src="../../js/vue.js"></script>

    

    <script>
      Vue.config.productionTip = false
      window.PAGE_TYPE = "cookbook"
    </script>

    <!-- ga -->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-46852172-3', 'cn.vuejs.org');
      ga('send', 'pageview');
    </script>
  </head>
  <body class="docs">
    <div id="mobile-bar">
      <a class="menu-button"></a>
      <a class="logo" href="../../index.html"></a>
    </div>
    <div id="header">
  <a id="logo" href="../../index.html">
    <img src="../../images/logo.png" alt="vue logo">
    <span>Vue.js</span>
  </a>
  <ul id="nav">
    <li>
  <form id="search-form">
    <input type="text" id="search-query-nav" class="search-query st-default-search-input" aria-label="搜索">
  </form>
</li>
<li class="nav-dropdown-container learn">
  <a class="nav-link current">学习</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><h4>文档</h4></li>
    <li>
      <ul>
        <li><a href="../guide/index.html" class="nav-link">教程</a></li>
        <li><a href="../api/index.html" class="nav-link">API</a></li>
        <li><a href="../style-guide/index.html" class="nav-link">风格指南</a></li>
        <li><a href="../examples/index.html" class="nav-link">示例</a></li>
        <li><a href="index.html" class="nav-link current">Cookbook</a></li>
      </ul>
    </li>
    <li><h4>视频教程</h4></li>
    <li>
      <ul>
        <li><a href="https://www.vuemastery.com/courses/" class="nav-link" target="_blank" rel="sponsored noopener">Vue Mastery (英文)</a></li>
        <li><a href="https://vueschool.io/?friend=vuejs&amp;utm_source=Vuejs.org&amp;utm_medium=Link&amp;utm_content=Navbar%20Dropdown" class="nav-link" target="_blank" rel="noopener">Vue School (英文)</a></li>
        <li><a href="https://learning.dcloud.io/" class="nav-link" target="_blank" rel="sponsored noopener">DCloud 视频教程</a></li>
      </ul>
    </li>
  </ul>
</li>

<li class="nav-dropdown-container ecosystem">
  <a class="nav-link">生态系统</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><h4>帮助</h4></li>
    <li><ul>
      <li><a href="https://forum.vuejs.org/" class="nav-link" target="_blank" rel="noopener">论坛</a></li>
      <li><a href="https://chat.vuejs.org/" class="nav-link" target="_blank" rel="noopener">聊天室</a></li>
      <li><a href="https://events.vuejs.org/meetups/" class="nav-link" target="_blank" rel="noopener">聚会</a></li>
    </ul></li>
    <li><h4>工具</h4></li>
    <li>
      <ul>
        <li><a href="https://github.com/vuejs/vue-devtools" class="nav-link" target="_blank" rel="noopener">Devtools</a></li>
        <li><a href="https://cli.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue CLI</a></li>
        <li><a href="https://vue-loader.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue Loader</a></li>
      </ul>
    </li>
    <li><h4>核心插件</h4></li>
    <li><ul>
      <li><a href="https://router.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue Router</a></li>
      <li><a href="https://vuex.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vuex</a></li>
      <li><a href="https://ssr.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue 服务端渲染</a></li>
    </ul></li>
    <li><h4>信息</h4></li>
    <li><ul>
      <li><a href="https://news.vuejs.org" class="nav-link" target="_blank" rel="noopener">周刊</a></li>
      <li><a href="https://github.com/vuejs/vue/projects/6" class="nav-link" target="_blank" rel="noopener">Roadmap</a></li>
      <li><a href="https://events.vuejs.org/" class="nav-link" target="_blank" rel="noopener">活动</a></li>
      <li><a href="https://twitter.com/vuejs" class="nav-link" target="_blank" rel="noopener">Twitter</a></li>
      <li><a href="https://medium.com/the-vue-point" class="nav-link" target="_blank" rel="noopener">博客</a></li>
      <li><a href="https://vuejobs.com/?ref=vuejs" class="nav-link" target="_blank" rel="noopener">工作</a></li>
    </ul></li>
  </ul>
</li>

<li>
  <a href="../guide/team.html" class="nav-link team">团队</a>
</li>
<li class="nav-dropdown-container resources">
  <a href="#" class="nav-link">资源列表</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="../../resources/partners.html" class="nav-link">合作伙伴</a></li>
    <li><a href="../../resources/themes.html" class="nav-link">主题</a></li>
    <li><a href="https://github.com/vuejs/awesome-vue" class="nav-link" target="_blank">Awesome Vue</a></li>
  </ul>
</li>

<!-- <li>
  <a href="/partners" class="nav-link ">Partners</a>
</li> -->
<li class="nav-dropdown-container support-vue">
  <a href="../../support-vuejs/index.html" class="nav-link">支持 Vue</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="../../support-vuejs/index.html#One-time-Donations" class="nav-link">一次性赞助</a></li>
    <li><a href="../../support-vuejs/index.html#Recurring-Pledges" class="nav-link">周期性赞助</a></li>
    <li><a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" rel="noopener" class="nav-link">贴纸</a></li>
    <li><a href="https://osholic.com/?utm_source=vue&amp;utm_medium=dropdown" rel="noopener" class="nav-link">周边</a></li>
    <li><a href="https://vue.threadless.com" target="_blank" rel="noopener" class="nav-link">T 恤商店</a></li>
  </ul>
</li>

<li class="nav-dropdown-container language">
  <a class="nav-link">多语言</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="https://vuejs.org/v2/cookbook/serverless-blog.html" class="nav-link" target="_blank" rel="noopener">English</a></li>
    <li><a href="https://jp.vuejs.org/v2/cookbook/serverless-blog.html" class="nav-link" target="_blank" rel="noopener">日本語</a></li>
    <li><a href="https://ru.vuejs.org/v2/cookbook/serverless-blog.html" class="nav-link" target="_blank" rel="noopener">Русский</a></li>
    <li><a href="https://kr.vuejs.org/v2/cookbook/serverless-blog.html" class="nav-link" target="_blank" rel="noopener">한국어</a></li>
    <li><a href="https://br.vuejs.org/v2/cookbook/serverless-blog.html" class="nav-link" target="_blank" rel="noopener">Português</a></li>
    <li><a href="https://fr.vuejs.org/v2/cookbook/serverless-blog.html" class="nav-link" target="_blank" rel="noopener">Français</a></li>
    <li><a href="https://vi.vuejs.org/v2/cookbook/serverless-blog.html" class="nav-link" target="_blank" rel="noopener">Tiếng Việt</a></li>
  </ul>
</li>

<li><a href="https://github.com/vuejs/cn.vuejs.org/" target="_blank" class="nav-link contribute">参与翻译</a></li>

  </ul>
</div>

    
      <div id="main" class="fix-sidebar">
        
          
  

<div class="sidebar">
  <div class="sidebar-inner">
    
    <div class="list">
      <div id="sidebar-sponsors-special">
  <div class="main-sponsor">
    <span>特别赞助商</span>
    <div>
    <a href="http://www.dcloud.io/?hmsr=vuejsorg&amp;hmpl=&amp;hmcu=&amp;hmkw=&amp;hmci=" target="_blank" rel="sponsored noopener" class="logo">
      <img src="../../images/dcloud.gif" alt="">
    </a>
    </div>
    <div>
    <a href="https://youzan.github.io/vant?source=vuejsorg" target="_blank" rel="sponsored noopener" class="logo">
      <img src="../../images/vant.png" alt="">
    </a>
    </div>
  </div>
</div>
<!--

<div id="sidebar-sponsors-platinum-left">
  <div class="main-sponsor">
    <span>白金赞助商</span>
    <div>
    <a href="https://vueschool.io/?utm_source=Vuejs.org&utm_medium=Banner&utm_campaign=Sponsored%20Banner&utm_content=V1" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/vueschool.png" alt="VueSchool">
    </a>
    <a href="https://vehikl.com/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/vehikl.png" alt="Vehikl">
    </a>
    <a href="https://www.nativescript.org/vue?utm_source=vue-js-org&utm_medium=website&utm_campaign=nativescript-awareness" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/nativescript.png" alt="NativeScript">
    </a>
    <a href="https://retool.com/?utm_source=sponsor&utm_campaign=vue" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/retool.png" alt="Retool">
    </a>
    </div>
  </div>
  <a class="become-backer" href="/support-vuejs">
    成为赞助者
  </a>
</div>


-->

      
        <h2>
          
          Cookbook<sup class="beta">beta</sup>
          
        </h2>
        <ul class="menu-root">
  
    
    
    <li>
      <a href="index.html" class="sidebar-link">介绍</a>
    </li>
  
    
    
    <li>
      <a href="adding-instance-properties.html" class="sidebar-link">添加实例属性</a>
    </li>
  
    
    
    <li>
      <a href="form-validation.html" class="sidebar-link">表单校验</a>
    </li>
  
    
    
    <li>
      <a href="editable-svg-icons.html" class="sidebar-link">可编辑的 SVG 图标系统</a>
    </li>
  
    
    
    <li>
      <a href="serverless-blog.html" class="sidebar-link current">创建一个基于 CMS 的博客</a>
    </li>
  
    
    
    <li>
      <a href="unit-testing-vue-components.html" class="sidebar-link">Vue 组件的单元测试</a>
    </li>
  
    
    
    <li>
      <a href="creating-custom-scroll-directives.html" class="sidebar-link">创建自定义滚动指令</a>
    </li>
  
    
    
    <li>
      <a href="debugging-in-vscode.html" class="sidebar-link">在 VS Code 中调试</a>
    </li>
  
    
    
    <li>
      <a href="using-axios-to-consume-apis.html" class="sidebar-link">使用 axios 访问 API</a>
    </li>
  
    
    
    <li>
      <a href="avoiding-memory-leaks.html" class="sidebar-link">避免内存泄漏</a>
    </li>
  
    
    
    <li>
      <a href="client-side-storage.html" class="sidebar-link">客户端存储</a>
    </li>
  
    
    
    <li>
      <a href="packaging-sfc-for-npm.html" class="sidebar-link">Packaging Vue Components for npm</a>
    </li>
  
    
    
    <li>
      <a href="dockerize-vuejs-app.html" class="sidebar-link">Dockerize Vue.js App</a>
    </li>
  
    
    
    <li>
      <a href="practical-use-of-scoped-slots.html" class="sidebar-link">Practical use of scoped slots with GoogleMaps</a>
    </li>
  
</ul>

      
    </div>
  </div>
</div>
<!--

<div id="sidebar-sponsors-platinum-right">
  <div class="main-sponsor">
    <span>白金赞助商</span>
    <div>
    <a href="https://vueschool.io/?utm_source=Vuejs.org&utm_medium=Banner&utm_campaign=Sponsored%20Banner&utm_content=V1" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/vueschool.png" alt="VueSchool">
    </a>
    <a href="https://vehikl.com/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/vehikl.png" alt="Vehikl">
    </a>
    <a href="https://www.nativescript.org/vue?utm_source=vue-js-org&utm_medium=website&utm_campaign=nativescript-awareness" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/nativescript.png" alt="NativeScript">
    </a>
    <a href="https://retool.com/?utm_source=sponsor&utm_campaign=vue" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/retool.png" alt="Retool">
    </a>
    </div>
  </div>
  <a class="become-backer" href="/support-vuejs">
    成为赞助者
  </a>
</div>


-->


<div class="content cookbook with-sidebar ">
  
    
      
<div id="ad">
  <span style="float: right;">广告</span>
  <a href="https://time.geekbang.org/column/intro/216?code=mM%2FdqOPks0VQ7EkYGPNisoEpHDsb0ViHBXYa%2Fyg02k8%3D&amp;utm_term=SPoster" target="_blank" rel="sponsored noopener">
    <img src="../../images/geekbang-ad.jpg">
  </a>
  <!-- <a href="https://ke.qq.com/course/392130?saleToken=992600&from=pclink" target="_blank" rel="sponsored noopener">
    <img src="/images/tencent-ad.png">
  </a> -->
  <!-- <script
    async
    type="text/javascript"
    src="//cdn.carbonads.com/carbon.js?serve=CKYIK2QU&placement=vuejsorg"
    id="_carbonads_js">
  </script> -->
</div>


    
  
  
    <h1>创建一个基于 CMS 的博客</h1>
  

  <div class="ad-pagetop">
  <a href="http://www.tp-shop.cn/index.php?http_referer=vuejs" target="_blank" rel="noopener noreferrer">
    <span class="name">TPshop</span>
    <span class="description">中国免费商城系统 - 搜豹商城系统 - 免费50小时Vue视频教程</span>
    <span class="action">立即查看 &gt;</span>
  </a>
</div>


  
    <p>恭喜你已经发布了你的 Vue.js 网站！现在你想要在网站上快速添加一个博客系统，但不想新起一台服务器去部署一个 WordPress 实例 (或任何基于数据库的 CMS)。你希望只添加一些 Vue.js 的博客组件和一些路由就能搞定对吧？你想要的就是直接消费你的 Vue.js 应用中的 API 来完全支撑起一个博客站。这份教程将会告诉你如何做到这一点，所以让我们来看看吧！</p>
<p>我们将会使用 Vue.js 快速构建一个基于 CMS 的博客站。它使用了 <a href="https://buttercms.com/" target="_blank" rel="noopener">ButterCMS</a>，一个 API 优先的 CMS，让你使用 ButterCMS 仪表盘管理内容并将我们的内容 API 集成到你的 Vue.js 应用中。你可以为一个新的项目或在一个已有的 Vue.js 项目使用 ButterCMS。</p>
<p><img src="https://user-images.githubusercontent.com/160873/36677285-648798e4-1ad3-11e8-9454-d22fca8280b7.png" alt="Butter 仪表盘" title="Butter Dashboard"></p>
<h2 id="安装"><a href="#%E5%AE%89%E8%A3%85" class="headerlink" title="安装"></a>安装</h2><p>在你的命令行中运行：</p>
<pre><code class="hljs bash">npm install buttercms --save</code></pre>
<p>Butter 也可以通过 CDN 加载：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdnjs.buttercms.com/buttercms-1.1.0.min.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></code></pre>
<h2 id="快速开始"><a href="#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B" class="headerlink" title="快速开始"></a>快速开始</h2><p>设置你的 API token：</p>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> butter = <span class="hljs-built_in">require</span>(<span class="hljs-string">'buttercms'</span>)(<span class="hljs-string">'your_api_token'</span>);</code></pre>
<p>使用 ES6：</p>
<pre><code class="hljs javascript"><span class="hljs-keyword">import</span> Butter <span class="hljs-keyword">from</span> <span class="hljs-string">'buttercms'</span>;
<span class="hljs-keyword">const</span> butter = Butter(<span class="hljs-string">'your_api_token'</span>);</code></pre>
<p>使用 CDN:</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdnjs.buttercms.com/buttercms-1.1.0.min.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
  <span class="hljs-keyword">var</span> butter = Butter(<span class="hljs-string">'your_api_token'</span>);
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></code></pre>
<p>将这个文件导入到任何你想使用 ButterCMS 的组件中。然后在浏览器的命令行中运行：</p>
<pre><code class="hljs javascript">butter.post.list(&#123;<span class="hljs-attr">page</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">page_size</span>: <span class="hljs-number">10</span>&#125;).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>&#123;
  <span class="hljs-built_in">console</span>.log(response)
&#125;)</code></pre>
<p>这个 API 请求会获取你的博客文章列表。你将会在请求的响应中看到你的账户的一篇示例博文。</p>
<h2 id="展示博文"><a href="#%E5%B1%95%E7%A4%BA%E5%8D%9A%E6%96%87" class="headerlink" title="展示博文"></a>展示博文</h2><p>为了展示博文，我们在应用中创建了一个 <code>/blog</code> 路由 (使用 Vue Router) 并从 Butter API 获取博文列表，同样的还创建了一个 <code>/blog/:slug</code> 路由来处理单篇博文。</p>
<p>你可以翻阅 ButterCMS <a href="https://buttercms.com/docs/api/?javascript#blog-posts" target="_blank" rel="noopener">API 参考文档</a> 来了解更多选项，比如按分类或作者过滤。请求的响应也会包含一些用在翻页导航上的元数据。</p>
<p><code>router/index.js:</code></p>
<pre><code class="hljs javascript"><span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>
<span class="hljs-keyword">import</span> Router <span class="hljs-keyword">from</span> <span class="hljs-string">'vue-router'</span>
<span class="hljs-keyword">import</span> BlogHome <span class="hljs-keyword">from</span> <span class="hljs-string">'@/components/BlogHome'</span>
<span class="hljs-keyword">import</span> BlogPost <span class="hljs-keyword">from</span> <span class="hljs-string">'@/components/BlogPost'</span>

Vue.use(Router)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">new</span> Router(&#123;
  <span class="hljs-attr">mode</span>: <span class="hljs-string">'history'</span>,
  <span class="hljs-attr">routes</span>: [
    &#123;
      <span class="hljs-attr">path</span>: <span class="hljs-string">'/blog/'</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">'blog-home'</span>,
      <span class="hljs-attr">component</span>: BlogHome
    &#125;,
    &#123;
      <span class="hljs-attr">path</span>: <span class="hljs-string">'/blog/:slug'</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">'blog-post'</span>,
      <span class="hljs-attr">component</span>: BlogPost
    &#125;
  ]
&#125;)</code></pre>
<p>然后创建 <code>components/BlogHome.vue</code> 作为你的博客首页，列出你最近的博文。</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
  <span class="hljs-keyword">import</span> &#123; butter &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'@/buttercms'</span>
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;
    <span class="hljs-attr">name</span>: <span class="hljs-string">'blog-home'</span>,
    data() &#123;
      <span class="hljs-keyword">return</span> &#123;
        <span class="hljs-attr">page_title</span>: <span class="hljs-string">'Blog'</span>,
        <span class="hljs-attr">posts</span>: []
      &#125;
    &#125;,
    <span class="hljs-attr">methods</span>: &#123;
      getPosts() &#123;
        butter.post.list(&#123;
          <span class="hljs-attr">page</span>: <span class="hljs-number">1</span>,
          <span class="hljs-attr">page_size</span>: <span class="hljs-number">10</span>
        &#125;).then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> &#123;
          <span class="hljs-keyword">this</span>.posts = res.data.data
        &#125;)
      &#125;
    &#125;,
    created() &#123;
      <span class="hljs-keyword">this</span>.getPosts()
    &#125;
  &#125;
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"blog-home"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>&#123;&#123; page_title &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-comment">&lt;!-- 创建 `v-for` 并为 Vue 应用一个 `key`，该示例使用了 `slug` 和 `index` 的组合。 --&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>
        <span class="hljs-attr">v-for</span>=<span class="hljs-string">"(post,index) in posts"</span>
        <span class="hljs-attr">:key</span>=<span class="hljs-string">"post.slug + '_' + index"</span>
      &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">:to</span>=<span class="hljs-string">"'/blog/' + post.slug"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">article</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"media"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">figure</span>&gt;</span>
              <span class="hljs-comment">&lt;!-- 使用 `:` 绑定结果 --&gt;</span>
              <span class="hljs-comment">&lt;!-- 使用一组 `v-if`/`else` 判断它们是否是 `featured_image` --&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">img</span>
                <span class="hljs-attr">v-if</span>=<span class="hljs-string">"post.featured_image"</span>
                <span class="hljs-attr">:src</span>=<span class="hljs-string">"post.featured_image"</span>
                <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>
              &gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">img</span>
                <span class="hljs-attr">v-else</span>
                <span class="hljs-attr">src</span>=<span class="hljs-string">"http://via.placeholder.com/250x250"</span>
                <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>
              &gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">figure</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>&#123;&#123; post.title &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>&#123;&#123; post.summary &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">article</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span></code></pre>
<p>这是它的样子 (注意为了快速设置样式，我们从 <a href="https://bulma.io/" target="_blank" rel="noopener">https://bulma.io/</a> 添加了 CSS)：</p>
<p><img src="https://user-images.githubusercontent.com/160873/36868500-1b22e374-1d5e-11e8-82a0-20c8dc312716.png" alt="buttercms-bloglist"></p>
<p>现在创建 <code>components/BlogPost.vue</code> 用来展示你的单篇博文页面。</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
  <span class="hljs-keyword">import</span> &#123; butter &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'@/buttercms'</span>
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;
    <span class="hljs-attr">name</span>: <span class="hljs-string">'blog-post'</span>,
    data() &#123;
      <span class="hljs-keyword">return</span> &#123;
        <span class="hljs-attr">post</span>: &#123;&#125;
      &#125;
    &#125;,
    <span class="hljs-attr">methods</span>: &#123;
      getPost() &#123;
        butter.post.retrieve(<span class="hljs-keyword">this</span>.$route.params.slug)
          .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> &#123;
            <span class="hljs-keyword">this</span>.post = res.data
          &#125;).catch(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> &#123;
            <span class="hljs-built_in">console</span>.log(res)
          &#125;)
      &#125;
    &#125;,
    created() &#123;
      <span class="hljs-keyword">this</span>.getPost()
    &#125;
  &#125;
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"blog-post"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>&#123;&#123; post.data.title &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span>&#123;&#123; post.data.author.first_name &#125;&#125; &#123;&#123; post.data.author.last_name &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-html</span>=<span class="hljs-string">"post.data.body"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span>
      <span class="hljs-attr">v-if</span>=<span class="hljs-string">"post.meta.previous_post"</span>
      <span class="hljs-attr">:to</span>=<span class="hljs-string">"/blog/ + post.meta.previous_post.slug"</span>
      <span class="hljs-attr">class</span>=<span class="hljs-string">"button"</span>
    &gt;</span>
      &#123;&#123; post.meta.previous_post.title &#125;&#125;
    <span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span>
      <span class="hljs-attr">v-if</span>=<span class="hljs-string">"post.meta.next_post"</span>
      <span class="hljs-attr">:to</span>=<span class="hljs-string">"/blog/ + post.meta.next_post.slug"</span>
      <span class="hljs-attr">class</span>=<span class="hljs-string">"button"</span>
    &gt;</span>
      &#123;&#123; post.meta.next_post.title &#125;&#125;
    <span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span></code></pre>
<p>预览效果如下：</p>
<p><img src="https://user-images.githubusercontent.com/160873/36868506-218c86b6-1d5e-11e8-8691-0409d91366d6.png" alt="buttercms-blogdetail"></p>
<p>现在我们的应用已经拉取了所有博文并且可以导航到每个独立的博文。但上一篇博文/下一篇博文的链接还不工作。</p>
<p>需要注意的一点是在通过参数控制路由时，比如当用户从 <code>/blog/foo</code> 导航至 <code>/blog/bar</code> 时，我们复用了相同的组件实例。因为这两个路由渲染了相同的组件，所以比销毁老实例再创建新实例的效率更高。</p>
<p class="tip">注意，用这种方式使用组件意味着这个组件的生命周期钩子将不会被调用。请移步 Vue Router 的文档了解<a href="https://router.vuejs.org/en/essentials/dynamic-matching.html" target="_blank" rel="noopener">动态路由匹配</a>。</p>

<p>为了解决这个问题，我们需要匹配 <code>$route</code> 对象并在路由发生变化的时候调用 <code>getPost()</code>。</p>
<p>更新 <code>components/BlogPost.vue</code> 中的 <code>&lt;script&gt;</code> 部分：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
  <span class="hljs-keyword">import</span> &#123; butter &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'@/buttercms'</span>
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;
    <span class="hljs-attr">name</span>: <span class="hljs-string">'blog-post'</span>,
    data() &#123;
      <span class="hljs-keyword">return</span> &#123;
        <span class="hljs-attr">post</span>: <span class="hljs-literal">null</span>
      &#125;
    &#125;,
    <span class="hljs-attr">methods</span>: &#123;
      getPost() &#123;
        butter.post.retrieve(<span class="hljs-keyword">this</span>.$route.params.slug)
          .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> &#123;
            <span class="hljs-keyword">this</span>.post = res.data
          &#125;).catch(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> &#123;
            <span class="hljs-built_in">console</span>.log(res)
          &#125;)
      &#125;
    &#125;,
    <span class="hljs-attr">watch</span>: &#123;
      <span class="hljs-attr">$route</span>: &#123;
        <span class="hljs-attr">immediate</span>: <span class="hljs-literal">true</span>,
        handler(to, <span class="hljs-keyword">from</span>) &#123;
          <span class="hljs-keyword">this</span>.getPost()
        &#125;
      &#125;
    &#125;
  &#125;
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></code></pre>
<p>现在你的应用就有了可工作的博客，你可以在 ButterCMS 仪表盘便捷地更新它。</p>
<h2 id="分类、标签和作者"><a href="#%E5%88%86%E7%B1%BB%E3%80%81%E6%A0%87%E7%AD%BE%E5%92%8C%E4%BD%9C%E8%80%85" class="headerlink" title="分类、标签和作者"></a>分类、标签和作者</h2><p>使用 Butter 关于分类、标签和作者的 API 来设置和过滤你的博客。</p>
<p>你可以移步到 ButterCMS API 参考文档来进一步了解这些对象：</p>
<ul>
<li><a href="https://buttercms.com/docs/api/?ruby#categories" target="_blank" rel="noopener">Categories</a></li>
<li><a href="https://buttercms.com/docs/api/?ruby#tags" target="_blank" rel="noopener">Tags</a></li>
<li><a href="https://buttercms.com/docs/api/?ruby#authors" target="_blank" rel="noopener">Authors</a></li>
</ul>
<p>这里有一个示例，列出所有分类并根据分类获取博文列表。在生命周期钩子 <code>created()</code> 中调用这些方法：</p>
<pre><code class="hljs javascript">methods: &#123;
  <span class="hljs-comment">// ...</span>
  getCategories() &#123;
    butter.category.list()
      .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> &#123;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'List of Categories:'</span>)
        <span class="hljs-built_in">console</span>.log(res.data.data)
      &#125;)
  &#125;,
  getPostsByCategory() &#123;
    butter.category.retrieve(<span class="hljs-string">'example-category'</span>, &#123;
        <span class="hljs-attr">include</span>: <span class="hljs-string">'recent_posts'</span>
      &#125;)
      .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> &#123;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Posts with specific category:'</span>)
        <span class="hljs-built_in">console</span>.log(res)
      &#125;)
  &#125;
&#125;,
created() &#123;
  <span class="hljs-comment">// ...</span>
  <span class="hljs-keyword">this</span>.getCategories()
  <span class="hljs-keyword">this</span>.getPostsByCategory()
&#125;</code></pre>
<h2 id="替代方案"><a href="#%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%A1%88" class="headerlink" title="替代方案"></a>替代方案</h2><p>有一个替代方案，尤其在你只喜欢写 Markdown 时适用，就是使用诸如 <a href="https://nuxtent-module.netlify.com/guide/writing/#async-components" target="_blank" rel="noopener">Nuxtent</a> 的工具。Nuxtent 允许你在 Markdown 文件内部使用 <code>Vue Component</code>。它类似一个静态站点工具 (例如 Jekyll)，让你在 Markdown 文件中撰写你的博文。Nuxtent 将 Vue.js 和 Markdown 很好地整合起来，让你完全生活在 Vue.js 的世界里。</p>
<h2 id="总结"><a href="#%E6%80%BB%E7%BB%93" class="headerlink" title="总结"></a>总结</h2><p>差不多就是这些了！现在你已经在自己的应用中拥有了一个可以正常工作的 CMS 博客。我们希望这份教程可以帮助你，使你的 Vue.js 开发体验更有乐趣 :)</p>

  
  
  <div class="footer">
      <script src="https://m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
<div id="bsa-native"></div>
<script>
_bsa.init('custom', 'CKYD62QM', 'placement:vuejsorg',
  {
    target: '#bsa-native',
    template: '<a class="native-box" href="##statlink##"><div class="native-sponsor">Sponsor</div><div class="native-text"><strong>##company##</strong> — ##description##</div></a>'
  }
);
</script>


    发现错误？想参与编辑？
    <a href="https://github.com/vuejs/cn.vuejs.org/blob/master/src/v2/cookbook/serverless-blog.md" rel="noopener" target="_blank">
      在 GitHub 上编辑此页！
    </a>
  </div>
</div>

        
      </div>
      <script src="../../js/smooth-scroll.min.js"></script>
    

    <!-- main custom script for sidebars, version selects etc. -->
    <script src="../../js/css.escape.js"></script>
    <script src="../../js/common.js"></script>

    <!-- search -->
    <link href="https://code.bdstatic.com/npm/docsearch.js@1.5.0/dist/cdn/docsearch.min.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="../../css/search.css">
    <script src="https://code.bdstatic.com/npm/docsearch.js@1.5.0/dist/cdn/docsearch.min.js"></script>
    <script>
    [
      '#search-query-nav',
      '#search-query-sidebar',
      '#search-query-menu'
    ].forEach(function (selector) {
      if (!document.querySelector(selector)) return
      // search index defaults to v2
      var match = window.location.pathname.match(/^\/(v\d+)/)
      var version = match ? match[1] : 'v2'
      docsearch({
      appId: 'BH4D9OD16A',
      apiKey: '5638280abff9d207417bb03be05f0b25',
      indexName: 'vuejs_cn2',
      inputSelector: selector,
      algoliaOptions: { facetFilters: ["version:" + version] },
      autocompleteOptions: { hint: false, appendTo: 'body'}
      })
    })
    </script>
  </body>
</html>
